﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xant.FlowEngine.Interface;

namespace Xant.FlowEngine.Core
{
    public abstract class ManualActivity: AbstractActivity
    {

        #region 变量
        public delegate void WorkitemInitializedHandler(ManualActivity activity);
        public WorkitemInitializedHandler WorkitemInitialized;

        private List<Workitem> workitems = new List<Workitem>();
        #endregion

        #region 属性
        public IReadOnlyList<Workitem> Workitems
        {
            get
            {
                return workitems.AsReadOnly();
            }
        }

        //public List<IActor> Participants { get; internal set; }
        /// <summary>
        /// 活动执行人
        /// </summary>
        public List<IActor> Performers { get; internal set; }

        /// <summary>
        /// 活动知会人(也可以考虑执行人和知会人放一个列表中，以Type来区分)
        /// </summary>
        public List<IActor> Notifiers { get; internal set; }
        #endregion

        #region 构造函数
        public ManualActivity(): base()
        {
        }

        public ManualActivity(string code): base(code, null)
        {
        }

        public ManualActivity(string code, string name): base(code, name)
        {
        }

        #endregion

        #region 业务函数
        protected void AddWorkitem(params Workitem[] items)
        {
            foreach (var item in items)
            {
                if (this.workitems.Contains(item))
                {
                    throw new ApplicationException("该工作项已经存在于活动工作项列表中，不能重复添加！");
                }
            }
            foreach (var item in items)
            {
                item.OwnerActivity = this;
                item.AfterAction += workitem_AfterAction;
                this.workitems.Add(item);
            }
        }

        #endregion

        #region 事件响应
        protected abstract void workitem_AfterAction(AbstractWorkitem workitem);

        #endregion

    }
}
